package Main;

import java.util.List;

import DDL.DataBase;
import DML.Table;

public class ParsingSql {
	// 用于分析sql语句
	public String parsingSql(String sqlSentence, List<List<String>> resultSet) {
		sqlSentence=sqlSentence.trim();
		String[] tempStringArray = sqlSentence.trim().split("\\s+");
		int returnvalue = 0;
		if (tempStringArray.length < 3) {
			System.out.println("Sql语句有错");
			return "Sql语句有错";
		}
		int dadatabseNamelength = tempStringArray[2].length();
		if (tempStringArray[0].equals("CREATE")
				|| tempStringArray[0].equals("create"))
			if (tempStringArray[1].equals("TABLE")
					|| tempStringArray[1].equals("table")) {
				Table table = new Table();
				returnvalue = table.creatTable(sqlSentence);
				switch (returnvalue) {
				case 1:
					return "建表成功";
				case -1:
					return "SQL语句不合法";
				case -2:
					return "没有这个类型";
				case -3:
					return "建表时错误";

				}
			} else if (tempStringArray[1].toLowerCase().equals("database")
					&& tempStringArray.length == 3
					&& tempStringArray[2].charAt(dadatabseNamelength - 1) == ';') {
				DataBase database = new DataBase();
				if (database.create(tempStringArray[2].substring(0,
						dadatabseNamelength - 1)))
					return "数据库创建成功";
				return "创建数据库失败";
			} else {
				System.out.println("sql语句有错");
				return "SQL语句不合法";
			}
		if (tempStringArray[0].equals("EDIT")
				|| tempStringArray[0].equals("edit"))
			if (tempStringArray[1].equals("TABLE")
					|| tempStringArray[1].equals("table")) {
				Table table = new Table();
				returnvalue = table.editTable(sqlSentence);
				switch (returnvalue) {
				case 1:
					return "修改表成功";
				case -1:
					return "SQL语句不合法";
				case -2:
					return "没有这个类型";
				case -3:
					return "修改表出错";

				}
			}
		if (tempStringArray[0].equals("RENAME")
				|| tempStringArray[0].equals("rename"))
		// if(tempStringArray[1].equals("TABLE")||tempStringArray[1].equals("table"))
		{
			Table table = new Table();
			returnvalue = table.renameTable(sqlSentence);
			switch (returnvalue) {
			case 1:
				return "修改表名成功";
			case -1:
				return "SQL语句不合法";
			case -2:
				return "重命名失败！";
			}
		}

		if (tempStringArray[0].equals("DROP")
				|| tempStringArray[0].equals("drop"))
			if (tempStringArray[1].equals("TABLE")
					|| tempStringArray[1].equals("table")) {
				Table table = new Table();
				returnvalue = table.deleteTable(sqlSentence);
				switch (returnvalue) {
				case 1:
					return "删除表名成功";
				case -1:
					return "SQL语句不合法";
				case -2:
					return "删除失败！";
				}
			} else if (tempStringArray[1].toLowerCase().equals("database")
					&& tempStringArray.length == 3
					&& tempStringArray[2].charAt(dadatabseNamelength - 1) == ';') {
				DataBase database = new DataBase();
				if (database.drop(tempStringArray[2].substring(0,
						dadatabseNamelength - 1)))
					return "数据库删除成功";
				return "删除数据库出错";
			} else {
				System.out.println("sql语句有错");
				return "SQL语句不合法";
			}

		if (tempStringArray[0].equals("SELECT")
				|| tempStringArray[0].equals("select")) {
			Table table = new Table();
			returnvalue = table.select(sqlSentence, resultSet);
			switch (returnvalue) {
			case 1:
				return "查询成功";
			case -1:
				return "没有这张表或数据库";
			case -2:
				return "没有这个字段";
			case -3:
				return "格式不匹配";
			case -4:
				return "SQL语句不合法";
			case -5:
				return "没有要查找的元组";
			}
		}
		if (tempStringArray[0].equals("UPDATE")
				|| tempStringArray[0].equals("update")) {
			Table table = new Table();
			returnvalue = table.update(sqlSentence);
			switch (returnvalue) {
			case 1:
				return "更新成功";
			case -1:
				return "没有这张表或数据库";
			case 0:
				return "更新失败！";
			case -2:
				return "没有这个字段";
			case -3:
				return "格式不匹配";
			case -4:
				return "SQL语句不合法";
			case -5:
				return "主键不能修改";
			}
		}
		if (tempStringArray[0].equals("INSERT")
				|| tempStringArray[0].equals("insert")) {
			Table table = new Table();
			returnvalue = table.insert(sqlSentence);
			switch (returnvalue) {
			case 1:
				return "插入成功";
			case -1:
				return "没有这张表或数据库";
			case 0:
				return "ww删除失败！";
			case -2:
				return "没有这个字段";
			case -3:
				return "格式不匹配";
			case -4:
				return "SQL语句不合法";
			case -5:
				return "存在不为空但是未未插入的字段";
			case -6:
				return "插入的主键值重复！";
			}
		}
		if (tempStringArray[0].equals("DELETE")
				|| tempStringArray[0].equals("delete")) {
			Table table = new Table();
			returnvalue = table.delete(sqlSentence);
			switch (returnvalue) {
			case 1:
				return "删除成功";
			case -1:
				return "没有这张表或数据库";
			case 0:
				return "ww删除失败！";
			case -2:
				return "没有这个字段";
			case -3:
				return "格式不匹配";
			case -4:
				return "SQL语句不合法";
			}
		}
		return "";
	}

}
